package org.ccay.demo.address.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Named;

import org.ccay.core.base.PageVO;
import org.ccay.core.base.PagedResult;
import org.ccay.core.base.RamPager;
import org.ccay.core.util.StringUtil;
import org.ccay.demo.address.dao.AddressDao;
import org.ccay.demo.address.service.IAddressService;
import org.ccay.demo.address.vo.CityVO;
import org.ccay.demo.address.vo.ProvinceVO;

@Named
public class AddressService implements IAddressService {

	@Override
	public List<ProvinceVO> listAllProvinces() {
		return AddressDao.listAllProvinces();
	}

	@Override
	public List<CityVO> listAllCities() {
		return AddressDao.listAllCities();
	}

	@Override
	public List<CityVO> listCitiesByProvincesId(Long provinceId) {
		return  AddressDao.listCitiesByProvincesId(provinceId);
	}

	@Override
	public PagedResult<ProvinceVO> listProvinces(ProvinceVO condtion, PageVO page) {
		List<ProvinceVO> all = AddressDao.listAllProvinces();
		List<ProvinceVO> filtrated = new  ArrayList<ProvinceVO>();
		//过滤
		if(condtion != null){
			for(ProvinceVO vo : all){
				boolean isMatch = true;
				if(condtion.getProvinceId() != null && !condtion.getProvinceId().equals(vo.getProvinceId())){
					isMatch = false;
				}
				if(!StringUtil.isNullOrEmpty(condtion.getProvinceName()) && !vo.getProvinceName().contains(condtion.getProvinceName().replace("%", ""))){
					isMatch = false;
				}
				if(isMatch){
					filtrated.add(vo);
				}
			}
		}
		//内存分页
		page.setTotalRows(filtrated.size());
		PagedResult<ProvinceVO> result = new PagedResult<ProvinceVO>();
		result.setPageVO(page);
		if(!filtrated.isEmpty()){
			RamPager<ProvinceVO> ramPager = new RamPager<ProvinceVO>(filtrated,page.getPageSize());
			result.setResult(ramPager.page(page.getCurPage()));
		}else{
			result.setResult(Collections.<ProvinceVO> emptyList());
		}
		return result;
	}

	@Override
	public List<ProvinceVO> listProvincesByIds(List<Long> provinceIds) {
		List<ProvinceVO> all = AddressDao.listAllProvinces();
		List<ProvinceVO> filtrated = new  ArrayList<ProvinceVO>();
		for(ProvinceVO vo : all){
			for(Long id:provinceIds){
				if(vo.getProvinceId().equals(id)){
					filtrated.add(vo);
				}
			}
		}
		return filtrated;
	}

	@Override
	public PagedResult<CityVO> listCities(CityVO condtion, PageVO page) {
		List<CityVO> all =AddressDao.listAllCities();
		List<CityVO> filtrated = new ArrayList<CityVO>();
		//过滤
		if(condtion != null){
			for(CityVO vo : all){
				boolean isMatch = true;
				if(condtion.getProvinceId() != null && !condtion.getProvinceId().equals(vo.getProvinceId())){
					isMatch = false;
				}
				if(condtion.getCityId() != null && !condtion.getCityId().equals(vo.getCityId())){
					isMatch = false;
				}
				if(!StringUtil.isNullOrEmpty(condtion.getCityName()) && !vo.getCityName().contains(condtion.getCityName().replace("%", ""))){
					isMatch = false;
				}
				if(isMatch){
					filtrated.add(vo);
				}
			}
		}
		//内存分页
		page.setTotalRows(filtrated.size());
		PagedResult<CityVO> result = new PagedResult<CityVO>();
		result.setPageVO(page);
		if(!filtrated.isEmpty()){
			RamPager<CityVO> ramPager = new RamPager<CityVO>(filtrated,page.getPageSize());
			result.setResult(ramPager.page(page.getCurPage()));
		}else{
			result.setResult(Collections.<CityVO> emptyList());
		}
		return result;
	}

	@Override
	public List<CityVO> listCitiesByIds(List<Long> provinceIds) {
		List<CityVO> all =AddressDao.listAllCities();
		List<CityVO> filtrated = new  ArrayList<CityVO>();
		for(CityVO vo : all){
			for(Long id:provinceIds){
				if(vo.getCityId().equals(id)){
					filtrated.add(vo);
				}
			}
		}
		return filtrated;
	}
}
